package by.grsu.kononovich.page.simple;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.kononovich.app.MyFirstWebApplication;
import by.grsu.kononovich.dao.impl.ClientDao;
import by.grsu.kononovich.entity.Client;

public class ClientsListPage extends WebPage {
	public ClientsListPage() {
		super();

		
		final ClientDao clientDao = new ClientDao(MyFirstWebApplication.DATA_XML_FOLDER);

		
		final List<Client> allClients = clientDao.getAll();

		
		add(new DataView<Client>("table-rows", new ListDataProvider<Client>(allClients)) {
			@Override
			protected void populateItem(final Item<Client> item) {
				final Client client = item.getModelObject();
				
				item.add(new Label("id", client.getId()));
				item.add(new Label("name", client.getName()));
				item.add(new Label("surname", client.getSurname()));
				item.add(new Label("pasport", client.getPasport()));

				
				item.add(new Link("editLink") {
					@Override
					public void onClick() {
						
						setResponsePage(new ClientEditPage(client));
						System.out.println("Client"+client.getName()+" "+client.getSurname()+" "+client.getPasport()+" edit");
					}
				});

				
				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
					
						clientDao.delete(client.getId());
						
						setResponsePage(new ClientsListPage());
						System.out.println("Client "+client.getName()+" "+client.getSurname()+" "+client.getPasport()+" deleted");
					}
				});
			}
		});

		
		add(new Link("createLink") {
			@Override
			public void onClick() {
				
				setResponsePage(new ClientEditPage(new Client()));
				System.out.println("button CREAT NEW CLIENT pressed");
			}
		});
		}

}
